package com.situ.player.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.situ.player.R;
import com.situ.player.service.MusicService;

import java.util.ArrayList;

public class MusicPlayActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "MusicPlayActivity";
   
    private static SeekBar seekBar;
    private static TextView tvProgress;
    private static TextView tvTotal;
    private ImageView ivPrevious;
    private ImageView ivPlay;
    private ImageView ivNext;

    // 要播放的歌曲的路径
    private String path;
    // 当前目录下面所有的歌曲
    private ArrayList<String> mp3List;
    // 当前正在播放的歌曲在mp3List中的索引
    private int currentMusicIndex;
    private MusicService.MusicControll musicControll;

    private ServiceConnection connection = new ServiceConnection(){

        // 当Service绑定上之后会回调这个方法
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG, "onServiceConnected");
            musicControll = (MusicService.MusicControll) service;
            musicControll.init(path);
        }

        // 当Service断开时候会回调这个方法
        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    public static Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            Bundle bundle = msg.getData();
            // 总时长
            int duration = bundle.getInt("duration");
            // 当前播放时长
            int currentPosition = bundle.getInt("currentPosition");
            seekBar.setMax(duration);
            seekBar.setProgress(currentPosition);

            // 显示总时长
            int minute = duration / 1000 / 60;
            int second = duration / 1000 % 60;
            tvTotal.setText(minute + ":" + second);

            // 显示播放时长
            int currentMinute = currentPosition / 1000 / 60;
            int currentSecond = currentPosition / 1000 % 60;
            tvProgress.setText(currentMinute + ":" + currentSecond);
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_play);
        initView();
        initData();

        // 绑定service
        Intent intent = new Intent(this, MusicService.class);
        bindService(intent, connection, BIND_AUTO_CREATE);
    }

    private void initData() {
        Intent intent = getIntent();
        path = intent.getStringExtra("path");
        Log.i(TAG, path);
        mp3List = (ArrayList<String>) intent.getSerializableExtra("mp3List");
        currentMusicIndex = mp3List.indexOf(path);
    }

    private void initView() {
        seekBar = findViewById(R.id.seekBar);
        tvProgress = findViewById(R.id.tv_progress);
        tvTotal = findViewById(R.id.tv_total);
        ivPrevious = findViewById(R.id.iv_previous);
        ivPlay = findViewById(R.id.iv_play);
        ivNext = findViewById(R.id.iv_next);
        ivPrevious.setOnClickListener(this);
        ivPlay.setOnClickListener(this);
        ivNext.setOnClickListener(this);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            // 停止滑动时候处理
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // 得到当前拖动停止的位置
                int progress = seekBar.getProgress();
                // 调用服务的seekTo方法改变音乐的进度
                musicControll.seekTo(progress);
            }

            // 开始滑动时候处理
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            // 滑动条变化时候吹
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                                          boolean fromUser) {
                // TODO Auto-generated method stub

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_previous:
                if (currentMusicIndex == 0) {
                    Toast.makeText(this, "当前已经是第一首", Toast.LENGTH_LONG).show();
                    return;
                }
                currentMusicIndex--;
                musicControll.init(mp3List.get(currentMusicIndex));
                break;
            case R.id.iv_play:
                if (musicControll.isPalying()) {
                    musicControll.pause();
                    ivPlay.setImageDrawable(getResources().getDrawable(R.drawable.playbar_pause));
                } else {
                    musicControll.play();
                    ivPlay.setImageDrawable(getResources().getDrawable(R.drawable.playbar_play));
                }
                break;
            case R.id.iv_next:
                if (currentMusicIndex == mp3List.size() - 1) {
                    Toast.makeText(this, "当前已经是最后一首", Toast.LENGTH_LONG).show();
                    return;
                }
                currentMusicIndex++;
                musicControll.init(mp3List.get(currentMusicIndex));
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 解绑Service
        musicControll.pause();
        // 解绑服务
        unbindService(connection);
        Intent intent = new Intent(this, MusicService.class);
        stopService(intent);
    }
}